// components/remark-up/remark-up.js
import { giveLikeApi, submitComment } from '../../utils/tools.js';
const app = getApp();
import Http from '../../utils/http.js';
const http = new Http();
Component({
    data: {
        flag: false,
        wrapAnimate: 'wrapAnimate',
        bgOpacity: 0,
        frameAnimate: 'frameAnimate',
        remarkName: '',
        remarkEdit: false,
        show: false,
        anonymous: true, //评论匿名
        remarkValue: '',
        actions: [
            {
                name: '删除',
                value: 'remark-delete',
                color: '#FF455E',
            },
        ],
        remarkId: '',
    },
    properties: {
        commentList: Array,
        isIphone: Boolean,
        review: String, //是否隐藏 审核字段
        commentcount: Number, //评论数
        commentType: String, //评论类型
        praiseType: String, //点赞类型
        praiseflg: String, //是否点赞
        praisecnt: Number, //点赞数
        placeHoldComment: String, //input显示
        eventId: String,
        tocommentid: String,
    },
    observers: {
        showRemark: function (showRemark) {
            if (showRemark) {
                this.showFrame();
            }
        },
        remarkEdit: function (remarkEdit) {
            this.triggerEvent('remarkEditChange', { remarkEdit: remarkEdit });
        },
    },
    methods: {
        // 显示评论弹框
        showFrame() {
            this.setData({
                flag: true,
                wrapAnimate: 'wrapAnimate',
                frameAnimate: 'frameAnimate',
            });
        },
        // 隐藏评论弹框
        hideFrame() {
            const _that = this;
            _that.setData({
                wrapAnimate: 'wrapAnimateOut',
                frameAnimate: 'frameAnimateOut',
            });
            // setTimeout(() => {
            _that.setData({
                flag: false,
            });
            // }, 400);
        },
        catchNone() {
            //阻止冒泡
        },
        _showEvent() {
            this.triggerEvent('showEvent');
        },
        _hideEvent() {
            this.triggerEvent('hideEvent');
        },
        remarkContent: function (e) {
            console.log(e.detail.battleTag);
            this.setData({
                remarkName: '@' + e.detail.battleTag,
            });
        },

        inputBtn(e) {
            this.setData({
                remarkEdit: true,
            });
        },
        // 隐藏匿名
        removeFocus() {
            this.setData({
                remarkEdit: false,
            });
        },
        // 切换匿名
        toggle() {
            this.setData({
                anonymous: !this.data.anonymous,
            });
        },
        remarkInput(e) {
            this.setData({
                remarkValue: e.detail.value,
            });
        },
        remarkFocus(e) {
            this.setData({
                remarkEdit: true,
            });
        },
        remarkBlur() {
            this.setData({
                remarkEdit: false,
            });
        },
        // 底部评论图标
        jumpOrRemark() {
            if (!this.data.flag) {
                this.showFrame();
            } else {
                this.hideFrame();
            }
            this.triggerEvent('jumpOrRemark');
        },
        //回复评论
        remarkInfo(e) {
            let _that = this;
            _that.remarkFocus();
            let name =
                e.detail.anonymousflg == '01' ? '匿名' : e.detail.battleTag;
            _that.setData({
                tocommentid: e.detail.contentid, //被回复的评论id,回复时传
                placeHoldComment: '@' + name, //评论/回复内容
            });
        },
        // 操作评论
        deleteRemark(e) {
            this.setData({
                show: true,
                remarkId: e.detail.id,
            });
        },

        // 选中后
        onSelect(e) {
            console.log(e);
            let _that = this;
            if (e.detail.value == 'remark-delete') {
                wx.showModal({
                    title: '提示',
                    content: '确认要删除该项吗？',
                    success: function (res) {
                        if (res.confirm) {
                            _that.onCancel();
                            _that.handleDeleteRemark();
                        } else {
                            _that.onCancel();
                            console.log('点击取消回调');
                        }
                    },
                });
            }
        },
        // 取消编辑
        onCancel() {
            this.setData({
                show: false,
                remarkEdit: false,
            });
            this.triggerEvent('jumpOrRemark');
        },

        handleDeleteRemark() {
            let _that = this;
            //评论删除请求
            http.getRequest(
                `cjMap/deleteComment?authId=${app.globalData.authId}&id=${_that.data.remarkId}`
            ).then((res) => {
                if (res.status == 0) {
                    wx.showToast({
                        title: '删除成功',
                        icon: 'none',
                        duration: 1500,
                    });
                    _that.triggerEvent('refreshRemark');
                }
            });
        },
        //提交评论
        submitComment() {
            let _that = this;
            let level = '00';
            if (!_that.data.remarkValue) return;
            if (_that.data.tocommentid) {
                level = '01';
            }
            submitComment(
                _that.data.tocommentid,
                _that.data.eventId,
                _that.data.commentType,
                level,
                _that.data.remarkValue,
                _that.data.anonymous ? '01' : '00'
            ).then((res) => {
                if (res.status == 0) {
                    _that.triggerEvent('refreshRemark');
                    this.setData({
                        remarkEdit: false,
                        remarkValue: '', //评论/回复内容
                    });
                }
            });
        },
        //点赞
        giveLike(e) {
            let _that = this;
            _that.setData({
                praisecnt:
                    _that.data.praiseflg == '00'
                        ? _that.data.praisecnt + 1
                        : _that.data.praisecnt - 1,
                praiseflg: _that.data.praiseflg == '00' ? '01' : '00',
            });
            giveLikeApi(
                _that.data.praiseType,
                _that.data.eventId,
                _that.data.praiseflg
            );
        },
    },
});
